package com.sm.li.controller;

import com.github.pagehelper.PageHelper;
import com.sm.li.common.http.AxiosResult;
import com.sm.li.common.page.PageBean;
import com.sm.li.common.perm.HasPerm;
import com.sm.li.common.valid.group.AddGroup;
import com.sm.li.common.valid.group.UpdateGroup;
import com.sm.li.controller.base.BaseController;
import com.sm.li.dto.RoleDTO;
import com.sm.li.entity.Role;
import com.sm.li.query.RoleQuery;
import com.sm.li.service.RoleService;
import com.sm.li.transfer.RoleTransfer;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("role")
public class RoleController extends BaseController {
    @Autowired
    private RoleService roleService;

    @Autowired
    private RoleTransfer roleTransfer;

    @GetMapping
    public AxiosResult<List<RoleDTO>> list() {
        return AxiosResult.success(roleTransfer.toDTO(roleService.list()));
    }

    @GetMapping("{id}")
    public AxiosResult<RoleDTO> findById(@PathVariable Long id) {
        return AxiosResult.success(roleTransfer.toDTO(roleService.findById(id)));
    }

    //分页条件查询
    @GetMapping("searchPage")
    public AxiosResult<PageBean<RoleDTO>> searchPage(RoleQuery RoleQuery) {
        //开启分页
        System.out.println(RoleQuery);
        PageHelper.startPage(RoleQuery.getCurrentPage(), RoleQuery.getPageSize());
        PageBean<RoleDTO> pageBean = roleService.searchPage(RoleQuery);
        return AxiosResult.success(pageBean);
    }

    //@Valid 开启后端表单校验
    @PostMapping
    @HasPerm(permSign = "system:role:add")
    public AxiosResult add(@Validated(AddGroup.class) @RequestBody Role Role) {

        return toAxios(roleService.add(Role));
    }

    @PutMapping
    @HasPerm(permSign = "system:role:edit")
    public AxiosResult<Void> update(@Validated(UpdateGroup.class) @RequestBody Role Role) {
        return toAxios(roleService.update(Role));

    }

    @DeleteMapping("{id}")
    @HasPerm(permSign = "system:role:delete")
    public AxiosResult<Void> deleteById(@PathVariable Long id) {

        return toAxios( roleService.deleteCascade(id));

    }

    @DeleteMapping("batch/{ids}")
    @HasPerm(permSign = "system:role:batch")
    public AxiosResult<Void> batchDeleteById(@PathVariable List<Long> ids) {

        return toAxios(roleService.batchCascadeDelete(ids));

    }

    //给角色赋予权限
    @PostMapping("{roleId}/menu/{menuIds}")
    @HasPerm(permSign = "system:role:menu")
    public AxiosResult<Void> setRoleMenu(@PathVariable Long roleId, @PathVariable List<Long> menuIds) {
        int row=roleService.setRoleMenu(roleId,menuIds);
        return toAxios(row);
    }
    //获得角色权限
    @GetMapping("{roleId}/menus")
    public AxiosResult<List<Long>> getMenusByRoleId(@PathVariable Long roleId){
       List<Long> menuIds = roleService.getMenusByRoleId(roleId);
       return AxiosResult.success(menuIds);
    }
}
